#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import os
import shutil
import string
import re
import xml.etree.ElementTree as etree
sys.path.append("../SGPythonUtils")
import ConfigFileEdit

namespace = "http://schemas.android.com/apk/res/android"

fourlib = "E:/SVN/SGSDK/SGSMS/FOUR"
backupdir = "E:/SVN/SGSDK/SGTools/SGAutoPack/autopack_v4_ppl2/temp"
gamesrcpth = "E:/SVN/SGSDK/SGGames/bubble2"

baidulib = "E:/SVN/SGSDK/SGAna/SGBAIDU/SGBAIDU"
jinlilib = "E:/SVN/SGSDK/SGAna/SGJINLI/SGJINLI"
oppolib = "E:/SVN/SGSDK/SGAna/SGOppo/SingleGame/SGOPPO"
vivolib = "E:/SVN/SGSDK/SGAna/SGVivo/Vivo_lib_2.0.9"
huaweilib = "E:/SVN/SGSDK/SGAna/SGHuaWei/SGHuaWei"
meizulib = "E:/SVN/SGSDK/SGAna/SGMEIZU/SGMEIZU"
qihulib = "E:/SVN/SGSDK/SGAna/SGQIHOO/SGQIHOO"
xiaomilib = "E:/SVN/SGSDK/SGAna/SGMI/SGMI"

#检查资源相关的目录是否存在
def checkDirExist(dir):
	if(False == os.path.exists(dir)):
		print( dir + " is not exist")
		raise( dir + " is not exist")

#copy文件
def copyFileSafety(backupfile,desfile):
	if( True == os.path.exists(backupfile) ):
		if( True == os.path.exists(desfile) ):
			os.remove(desfile)
		shutil.copyfile(backupfile,desfile)

#删除文件
def deleteFileSafety(filename):
	if( True == os.path.exists(filename) ):
		os.remove(filename)

def read_xml(xml_file):
	tree = etree.parse(xml_file)
	return tree

def parse_attribute_name(attribute_name):
	ns = {
			'android:value' : '{%s}value' % namespace,
			'android:name' : '{%s}name' % namespace
		}
	return ns[attribute_name]

#修改manifest中的mete-data
def run(src_xml,save_file,dataMap):
	if( False == os.path.exists(src_xml) ):
		raise Exception(src_xml + "文件不存在，请检查!")
	if( False == os.path.exists(save_file) ):
		raise Exception(save_file + "文件不存在，请检查!")

	etree.register_namespace('android', namespace)
	
	tree = read_xml(src_xml)
	root = tree.getroot()
	
	for elem in root.findall('application/meta-data'):
		att = elem.get("{%s}name" % namespace)
		if att in dataMap:
			elem.set('{%s}value'% namespace, dataMap[att])

	tree.write(save_file, "utf-8")

#Baidu
def changeBaiduMetaData():
	checkDirExist(baidulib)
	fileSrcName = baidulib + "/AndroidManifest.xml"
	backupFile = baidulib + "/AndroidManifest.xml_bak"

	keyvaluemap = {}
	keyvaluemap['dksdk_appid'] = "\ 6653996"
	keyvaluemap['dksdk_appkey'] = "zcqELWjnrixQhACVdxszfg3o"
	keyvaluemap['dksdk_appsecret'] = "4z7QfajPtzTYnUbhwUw2Q7f8gaP6Hwzo"
	keyvaluemap['dksdk_ver'] = "208"

	copyFileSafety(fileSrcName,backupFile)
	run(fileSrcName,fileSrcName,keyvaluemap)

def recoveryBaiduMetaData():
	checkDirExist(baidulib)

	fileSrcName = baidulib + "/AndroidManifest.xml"
	backupFile = baidulib + "/AndroidManifest.xml_bak"

	copyFileSafety(backupFile,fileSrcName)
	deleteFileSafety(backupFile)

#金立
def changeJinliMetaData():
	checkDirExist(jinlilib)
	fileSrcName = jinlilib + "/AndroidManifest.xml"
	backupFile = jinlilib + "/AndroidManifest.xml_bak"

	keyvaluemap = {}
	keyvaluemap['API_KEY'] = ""
	keyvaluemap['PRIVATE_KEY'] = ""

#	copyFileSafety(fileSrcName,backupFile)
#	run(fileSrcName,fileSrcName,keyvaluemap)

def recoveryJinliMetaData():
	checkDirExist(jinlilib)

	fileSrcName = jinlilib + "/AndroidManifest.xml"
	backupFile = jinlilib + "/AndroidManifest.xml_bak"

#	copyFileSafety(backupFile,fileSrcName)
#	deleteFileSafety(backupFile)

#oppo
def changeOppoMetaData():
	checkDirExist(oppolib)
	fileSrcName = oppolib + "/AndroidManifest.xml"
	backupFile = oppolib + "/AndroidManifest.xml_bak"

	keyvaluemap = {}
	keyvaluemap['app_key'] = "12grrl25yx8g40Ogkk8kwwGoW"
	keyvaluemap['app_secret'] = "Bcf90344Ab56D39cfB3Cb808e07ACbE7"

	copyFileSafety(fileSrcName,backupFile)
	run(fileSrcName,fileSrcName,keyvaluemap)

def recoveryOppoMetaData():
	checkDirExist(oppolib)

	fileSrcName = oppolib + "/AndroidManifest.xml"
	backupFile = oppolib + "/AndroidManifest.xml_bak"

	copyFileSafety(backupFile,fileSrcName)
	deleteFileSafety(backupFile)

#vivo
def changeVivoMetaData():
	checkDirExist(vivolib)
	fileSrcName = vivolib + "/AndroidManifest.xml"
	backupFile = vivolib + "/AndroidManifest.xml_bak"

	keyvaluemap = {}
	keyvaluemap['STORE_ID'] = "\ 20140218104717392157"
	keyvaluemap['SIGN_KEY'] = "\ 20140218104717278498"
	keyvaluemap['APP_ID'] = "1065c19e0b78ea7950615d998bf63b7e"

	copyFileSafety(fileSrcName,backupFile)
	run(fileSrcName,fileSrcName,keyvaluemap)

def recoveryVivoMetaData():
	checkDirExist(vivolib)

	fileSrcName = vivolib + "/AndroidManifest.xml"
	backupFile = vivolib + "/AndroidManifest.xml_bak"

	copyFileSafety(backupFile,fileSrcName)
	deleteFileSafety(backupFile)

#huawei
def changeHuaweiMetaData():
	checkDirExist(huaweilib)
	fileSrcName = huaweilib + "/AndroidManifest.xml"
	backupFile = huaweilib + "/AndroidManifest.xml_bak"

	keyvaluemap = {}
	keyvaluemap['CHANNEL_PAY_APPID'] = "\ 10344735"
	keyvaluemap['CHANNEL_PAY_PAYID'] = "\ 900086000034703063"
	keyvaluemap['CHANNEL_PAY_BUOY_SECRET'] = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALQ8JRusTb3swpvmeOXbVjuJmf3e8PMbwhr5670hI8IgrYykszXBNOXCSaysr6m6MFw8zRG4BPDxFmauBYMVWZKpcce/lKfmw8s9DwYXQ+weuqU2txQjiR2jIm4lbn0NW0bT08TjgDM738NSHVyvGOktAmetJHas3Pb48/GBLxuRAgMBAAECgYA2Qo0hYaQd83M7IA1xlbDGR8y2YzHC3q53FJpl2946CUB2QPc6zamxKeaoN2zC+iyo+Bz8opvKNap6yxUZu4m1Xc0wGYYJpqgwQ0/ERkffFZsXceCcSPoOnmRf3Hm+X+fhcJQH/vG4oz56TahN7Ud2czsD4dEzA8mIwj9kQ88wAQJBAOkbfUeJSv8m/WLnjgYWs/IsfCCsacc+NVo+RfYrmDkhzKZpQpYkZo8lXKqc99tL9fBJ/5wx67NLqY+A6EGq/hECQQDF72YstXO7ZS48+P9R0Sfo89H79fsfkHxGDL1wm/8LGyz+w6QTrLfS9uWnSTqV6r6geve3Z5611IG0ZFdyYsWBAkBRtW0IcjtzjHss+/VwdOzGaoln+odpa4jQBRwI2ecVv584hZTtg/zfGD2PIE5Q9CNVAtsyKN1xj4fz4T+IJSPBAkB/4apb3GgHfNmg3ybmT4G/ijfy1/wFaq4/+98xMDhZjpdFilUpuP+TN50sta06Ss4alRgS0MnDpKagS+wLpTEBAkEAwskgpOYlOo7Idl2PBAMfSesqeBSegXq5y8QgQ48I+ue4VOtmJlMfy4FZ7kwWQ61yvBlxfLoLeVKwL69QMPxRVg=="
	keyvaluemap['CHANNEL_PAY_RSA_PUBLIC'] = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAss3lDFjSyDUHIIcuz2Z344Eo6ZsfNsVboIr/NJBOeP6sXj5iZDiL7F3cjyMU7PULpSuP8anGTUlhw11Ttim8UafD75g285cE7ewnFv6C+VEzBva2g35fE/xT9zh+XcBIu95R2a7OZtyuQLVjZ6SMbdYlzejOLy5RkXFvCkuYB3jU6fOaAxruUm09gYDjyd4587eXD6VT2jMCn5HeQCOwNDCbRgXZZpd33llYAzT+K52DacvXl6g4dhflDkMSar/MFeViuptILmrWNAocOnK8xq+rbZk3v8Q85xIXk3FloMH375yyY19rwHxQD1AhOHGDtKtxYb44veZ8z5A+xu4keQIDAQAB"

	copyFileSafety(fileSrcName,backupFile)
	run(fileSrcName,fileSrcName,keyvaluemap)

def recoveryHuaweiMetaData():
	checkDirExist(huaweilib)

	fileSrcName = huaweilib + "/AndroidManifest.xml"
	backupFile = huaweilib + "/AndroidManifest.xml_bak"

	copyFileSafety(backupFile,fileSrcName)
	deleteFileSafety(backupFile)

#meizu
def changeMeizuMetaData():
	checkDirExist(meizulib)
	fileSrcName = meizulib + "/AndroidManifest.xml"
	backupFile = meizulib + "/AndroidManifest.xml_bak"

	keyvaluemap = {}
	keyvaluemap['CHANNEL_PAY_APPID'] = "\ 2642303"
	keyvaluemap['CHANNEL_PAY_APPKEY'] = "6eaccb8c77514a2e9ff5ee4ad34599cd"
	keyvaluemap['CHANNEL_PAY_APPSECRET'] = "6gE5dTLSwdI3HP6c1HPP5Aj0D8e7hQhK"
	keyvaluemap['PS_WXLOGIN'] = "false"

	copyFileSafety(fileSrcName,backupFile)
	run(fileSrcName,fileSrcName,keyvaluemap)
	
	
def recoveryMeizuMetaData():
	checkDirExist(meizulib)

	fileSrcName = meizulib + "/AndroidManifest.xml"
	backupFile = meizulib + "/AndroidManifest.xml_bak"

	copyFileSafety(backupFile,fileSrcName)
	deleteFileSafety(backupFile)

#qihu
def changeQihuMetaData():
	checkDirExist(qihulib)
	fileSrcName = qihulib + "/AndroidManifest.xml"
	backupFile = qihulib + "/AndroidManifest.xml_bak"

	keyvaluemap = {}
	keyvaluemap['QHOPENSDK_APPKEY'] = "24ec92a230b0d8c23364b1d15c8711a4"
	keyvaluemap['QHOPENSDK_PRIVATEKEY'] = "60c2047415c91976bfc935153da61224"
	keyvaluemap['QHOPENSDK_APPID'] = "202671511"

	copyFileSafety(fileSrcName,backupFile)
	run(fileSrcName,fileSrcName,keyvaluemap)


def recoveryQihuMetaData():
	checkDirExist(qihulib)

	fileSrcName = qihulib + "/AndroidManifest.xml"
	backupFile = qihulib + "/AndroidManifest.xml_bak"

	copyFileSafety(backupFile,fileSrcName)
	deleteFileSafety(backupFile)

#小米
def changeXiaomiMetaData():
	checkDirExist(xiaomilib)
	fileSrcName = xiaomilib + "/AndroidManifest.xml"
	backupFile = xiaomilib + "/AndroidManifest.xml_bak"

	keyvaluemap = {}
	keyvaluemap['CHANNEL_PAY_APPID'] = "\ 2882303761517384378"
	keyvaluemap['CHANNEL_PAY_APPKEY'] = "\ 5701738431378"

	copyFileSafety(fileSrcName,backupFile)
	run(fileSrcName,fileSrcName,keyvaluemap)

def recoveryXiaomiMetaData():
	checkDirExist(xiaomilib)

	fileSrcName = xiaomilib + "/AndroidManifest.xml"
	backupFile = xiaomilib + "/AndroidManifest.xml_bak"

	copyFileSafety(backupFile,fileSrcName)
	deleteFileSafety(backupFile)

#运营商默认配置
def setDefaultSmsLib():
	projectfile = fourlib + "/project.properties"
	liblist = [
		'../../SGUtil/SGSDKUtils_Library',
		'../SmsLib/sms_lib_ayx_4.2.0',
		'../SmsLib/sms_lib_lt_4.0.2',
		'../SmsLib/sms_lib_mm_3.1.10'
	]
	ConfigFileEdit.setProjectRefLib(projectfile,liblist)
	
def recoverySmsLib():
	pass

#替换payTypeConfig.xml和serverControl.xml
def setPayTypeAndServerControl():
	gamePayTypeConfigFile = gamesrcpth + "/assets/soulgame/payTypeConfig.xml"
	gameServerControlFile = gamesrcpth + "/assets/soulgame/ServerControl.xml"
	backupPayTypeConfigFile = backupdir + "/backup_payTypeConfig.xml"
	backupServerControlFile = backupdir + "/backup_ServerControl.xml"
	
	copyFileSafety(backupPayTypeConfigFile,gamePayTypeConfigFile)
	copyFileSafety(backupServerControlFile,gameServerControlFile)

def recoveryPayTypeAndServerControl():
	pass

#调整资源
def gameCustomOperBegin():
	setDefaultSmsLib()
	setPayTypeAndServerControl()
	changeBaiduMetaData()
	changeJinliMetaData()
	changeOppoMetaData()
	changeVivoMetaData()
	changeHuaweiMetaData()
	changeMeizuMetaData()
	changeQihuMetaData()
	changeXiaomiMetaData()

#恢复资源
def gameCustomOperEnd():
	recoverySmsLib()
	recoveryPayTypeAndServerControl()
	recoveryBaiduMetaData()
	recoveryJinliMetaData()
	recoveryOppoMetaData()
	recoveryVivoMetaData()
	recoveryHuaweiMetaData()
	recoveryMeizuMetaData()
	recoveryQihuMetaData()
	recoveryXiaomiMetaData()

if __name__=="__main__":
	gameCustomOperBegin()
	gameCustomOperEnd()
